package edu.cs346.group5.ui.adminpages;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.JButton;

import edu.cs346.group5.logic.AddViewTopic;
import edu.cs346.group5.ui.ButtonColumn;
import edu.cs346.group5.ui.MenuFactory;

import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class Categories extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textField;
	private JTable table_1;
	private JScrollPane scrollPane;

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 */
	public Categories() {
		
		setJMenuBar(MenuFactory.getMenu(this));
		
		setTitle("Admin Category Management");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		textField = new JTextField();
		textField.setColumns(10);

		JButton btnNewButton = new JButton("Add Category");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (textField.getText().equals(""))
					return;
				AddViewTopic.AddCategory(textField.getText());
				refreshTable();
			}
		});

		scrollPane = new JScrollPane();

		refreshTable();
		// JScrollPane tablePane = new JScrollPane(table_1);

		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(gl_contentPane
				.createParallelGroup(Alignment.TRAILING)
				.addGroup(
						gl_contentPane
								.createSequentialGroup()
								.addComponent(textField,
										GroupLayout.PREFERRED_SIZE, 314,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED,
										11, Short.MAX_VALUE)
								.addComponent(btnNewButton))
				.addComponent(scrollPane, Alignment.LEADING,
						GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE));
		gl_contentPane
				.setVerticalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																textField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																btnNewButton))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addComponent(scrollPane,
												GroupLayout.DEFAULT_SIZE, 223,
												Short.MAX_VALUE)));
		contentPane.setLayout(gl_contentPane);

		// setJMenuBar(MenuFactory.getMenu(this));
	}

	private void refreshTable() {

		Object[] columnNames = new Object[] { "Category Name", "" };

		List<HashMap<String, Object>> results = AddViewTopic.GetAllCategories();
		Object[][] data = new Object[results.size()][2];
		for (int i = 0; i < results.size(); i++) {
			data[i][0] = (results.get(i).get("category_name").toString());
			data[i][1] = "Delete";
		}

		Action delete = new AbstractAction() {
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent e) {
				int modelRow = Integer.valueOf(e.getActionCommand());
				String data = table_1.getModel().getValueAt(modelRow, 0).toString();
				AddViewTopic.ArchiveCategory(data);
				refreshTable();
			}
		};

		table_1 = new JTable(data, columnNames);
		scrollPane.setViewportView(table_1);

		table_1.setRowHeight(16);

		@SuppressWarnings("unused")
		ButtonColumn buttonColumn = new ButtonColumn(table_1, delete, 1);
	}
}